package jmine.tec.utils.reflection.fast;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * {@link FastClass} abstrato
 * 
 * @author takeshi
 */
public abstract class AbstractFastClass implements FastClass {

    /**
     * {@inheritDoc}
     */
    public FastMethod findMethod(String name, java.lang.Class<?>... types) {
        for (FastMethod m : this.getMethods()) {
            if (m.getName().equals(name) && Arrays.equals(types, m.getArgumentTypes())) {
                return m;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public FastMethod findMethodFor(Method method) {
        return this.findMethod(method.getName(), method.getParameterTypes());
    }

    /**
     * {@inheritDoc}
     */
    public FastConstructor findConstructor(Class<?>... types) {
        for (FastConstructor fc : this.getConstructors()) {
            if (Arrays.equals(fc.getParamTypes(), types)) {
                return fc;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public FastConstructor findConstructor(Constructor<?> ctor) {
        if (ctor.getDeclaringClass() != this.getType()) {
            throw new IllegalArgumentException("incompatible constructor:" + ctor + " for type: " + this.getType());
        }
        return this.findConstructor(ctor.getParameterTypes());
    }

    /**
     * {@inheritDoc}
     */
    public FastMethod findMethodBestMatch(String methodName, Class<?>[] params) {
        Collection<? extends FastMethod> methods = this.getMethods();
        List<FastMethod> candidates = new ArrayList<FastMethod>();
        for (FastMethod fastMethod : methods) {
            if (fastMethod.getName().equals(methodName) && fastMethod.getArity() == params.length) {
                candidates.add(fastMethod);
            }
        }
        if (candidates.size() == 0) {
            return null;
        }
        for (MatchLevel level : MatchLevel.values()) {
            for (FastMethod fastMethod : candidates) {
                if (level.matchesAll(fastMethod.getArgumentTypes(), params)) {
                    return fastMethod;
                }
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public Object invokeDynamic(String methodName, Object target, Object... params) throws NoSuchMethodException, Exception {
        FastMethod fm = this.findBestMethodFor(methodName, params);
        if (fm == null) {
            throw new NoSuchMethodException("No match found for name: " + methodName + " with arguments: " + Arrays.toString(params));
        }
        return fm.invoke(target, params);
    }

    /**
     * {@inheritDoc}
     */
    public FastMethod findBestMethodFor(String methodName, Object... params) {
        Class<?>[] types = new Class<?>[params.length];
        for (int i = 0; i < types.length; i++) {
            types[i] = params[i] != null ? params[i].getClass() : null;
        }
        return this.findMethodBestMatch(methodName, types);
    }

    /**
     * @param prim {@link Class}
     * @return {@link Class}
     */
    public static Class<?> promotePrimitive(Class<?> prim) {
        if (prim.isPrimitive()) {
            if (int.class == prim) {
                return Integer.class;
            }
            if (boolean.class == prim) {
                return Boolean.class;
            }
            if (short.class == prim) {
                return Short.class;
            }
            if (char.class == prim) {
                return Character.class;
            }
            if (float.class == prim) {
                return Float.class;
            }
            if (double.class == prim) {
                return Double.class;
            }
            if (long.class == prim) {
                return Long.class;
            }
            if (byte.class == prim) {
                return Byte.class;
            }
        }
        return prim;
    }
}
